import time
import unittest
from concurrent.futures.thread import ThreadPoolExecutor

suite=unittest.defaultTestLoader.discover(r"D:\lemonclass\高级编程\多进程和队列\testcase")
print(suite)
# 拆分收集到的测试套件
suite_list=[]
# 遍历测试套件。遍历出来的测试模块级别的套件
for item in suite:
    print(item)
    for sui_cls in item:
        # 将所有的测试类级别的套件加入到suit_list列表中
        suite_list.append(sui_cls)

print("suite_list",suite_list)
def execute(suite:unittest.TestSuite):
    result=unittest.TestResult()
    res=suite.run(result)
    print(res)
    res=dict(
        fail=len(res.failures),
        error=len(res.errors),
        skip=len(res.skipped),
        all_test=res.testsRun
    )
    return res

# 创建线程去执行测试用例
st_time=time.time()
with ThreadPoolExecutor(max_workers=4) as pool:
    result=pool.map(execute,suite_list)
    res={'fail':0,'error':0,'skip':0,'all_test':0}
    for item in list(result):
        res['fail']+=item['fail']
        res['error']+=item['error']
        res['skip']+=item['skip']
        res['all_test']+=item['all_test']
    print(res)
end_time=time.time()
print(end_time-st_time)